// 402. 移掉 K 位数字
// 单调栈
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
  string removeKdigits(string num, int k) {
    string stk;
    int leave = num.size()-k;
    for(auto c:num) {
      while(k&&!stk.empty()&& stk.back()>c){
        stk.pop_back();
        k--;
      }
      stk.push_back(c);
    }
    stk.resize(leave);
    while(stk.size()>1&&stk[0]=='0')
      stk.erase(0,1);
    return stk.empty()?"0":stk;
  }
};

int main_re(){
  string s = "123456";
  s.resize(3);
  cout << s << endl;
  cout << Solution().removeKdigits("1432219", 3) << endl;
  return 0;
}
